<%-- 
/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
--%>

<%@page import="edu.fpt.hib.RequestServicesId"%>
<%@page import="edu.fpt.hib.Services"%>
<%@page import="edu.fpt.dal.ServicesDAL"%>
<%@page import="edu.fpt.dal.RequestServicesDAL"%>
<%@page import="edu.fpt.hib.RequestServices"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.StringTokenizer"%>
<%@page import="edu.fpt.dal.ApartmentDAL"%>
<%@page import="edu.fpt.hib.Apartment"%>
<%@page import="edu.fpt.dal.UsersDAL"%>
<%@page import="edu.fpt.hib.Users"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Register for using service</title>
        <style type="text/css"> <!--@import url("../style.css");--></style>
        <script>function goBack()  {  window.history.back()  }</script>
    </head>
    <body>
        <%@include file="template/header.jsp" %>
        <% Users users = UsersDAL.getUsersHQL(session.getAttribute("currentUser").toString());
            ArrayList<Services> listSer = ServicesDAL.listNotUsing(users.getUserId());%>
        <div id="page">
            <% if (request.getParameter("servicesid") == null) {
                    if (listSer.size()>1) {%>
            <h2 class="title">Registration for services</h2>
            <form method="POST" action="registerservices.jsp">
                <table border="0">
                    <tr>
                        <td align="right">Register for services</td>
                        <td><select size="1" name="servicesid">

                                <%for (int i = 0; i < listSer.size(); i++) {%>
                                <option value="<%=listSer.get(i).getServiceId()%> "><%=listSer.get(i).getServiceName()%></option>
                                <%}%></select></td>
                    </tr>
                    <tr>
                        <td align="right">&nbsp;</td>
                        <td>
                            <p align="center"><input class="searchsubmit" type="submit" value="Register" name="B1"><input class="searchsubmit" type="reset" value="Reset" name="B2"></td>
                    </tr>
                </table>
            </form>
            <% } else {
            %>
            <h4>There are no available services for you!</h4><input class="searchsubmit" type='button' value='Back' onclick='goBack()'>
            <%                    }
                } else {
                    String services = request.getParameter("servicesid");
                    Services ser = ServicesDAL.getByServicesID(services);
                    RequestServicesId requestServicesId = new RequestServicesId(users.getUserId(), services);
                    if (RequestServicesDAL.exist(requestServicesId)) {
                        response.sendRedirect("myservices.jsp");
                    } else {
                        RequestServices req = new RequestServices(requestServicesId, ser, users, "Pend");
                        RequestServicesDAL.insert(req);
                        response.sendRedirect("myservices.jsp");
                    }


                }
            %>
        </div>
        <%@include file="template/footer.jsp" %>
    </body>
</html>
